package sc.common.test.hopper.handler;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

import sc.common.test.hopper.model.MyMessage;

public class MyMessageEncoder extends SimpleChannelHandler {
	@Override
	public void writeRequested(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		MyMessage msg = (MyMessage)e.getMessage();
		
		ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
		buf.writeInt(0);
		buf.writeInt(msg.getId());
		buf.writeInt(msg.getVerb().getType());
		
		int size = buf.readableBytes();
		buf.setInt(0, size);

		Channels.write(ctx, e.getFuture(), buf);
	}
}
